package com.chookss.home.logContacts;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.home.entity.ReadEntity;
import com.chookss.mine.personal.PersonalHomeActivity;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.tools.okgoconfig.model.LzyResponse;
import com.chookss.view.CircleImageView;
import com.google.gson.reflect.TypeToken;
import com.lvgroup.hospital.base.BaseAct;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;
import io.reactivex.annotations.Nullable;

public class LogReadsActivity extends BaseAct {


    @BindView(R.id.common_title_txt)
    TextView commonTitleTxt;
    @BindView(R.id.rvView)
    RecyclerView recyclerView;
    @BindView(R.id.srlView)
    SmartRefreshLayout smrRf;
    @BindView(R.id.ivNone)
    ImageView ivNone;
    @BindView(R.id.tvNone)
    TextView tvNone;
    @BindView(R.id.tvNone2)
    TextView tvNone2;
    @BindView(R.id.llNone)
    LinearLayout llNone;
    @BindView(R.id.llBg)
    LinearLayout llBg;
    private ReadAdapter adapter;
    private int currentPage = 1;
    private boolean isEnd = false;//是否到底
    private List<ReadEntity> list = new ArrayList<>();
private String reportCode;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        setContentView(R.layout.common_srlview_center);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        initState();
        reportCode = getIntent().getStringExtra("reportCode");
        commonTitleTxt.setText("最近浏览的人");
        ivNone.setBackgroundResource(R.drawable.icon_default_no_common);
        tvNone.setText("暂无数据");
        recyclerView.setPadding(0,Utils.dip2px(this,17),0,0);
       LinearLayout.LayoutParams params= (LinearLayout.LayoutParams) smrRf.getLayoutParams();
        params.setMargins(0,Utils.dip2px(this,12),0,0);
        smrRf.setLayoutParams(params);
        smrRf.setBackgroundColor(getResources().getColor(R.color.white));
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager
                .VERTICAL, false));
        adapter = new ReadAdapter(R.layout.item_log_read, this, list);
        recyclerView.setAdapter(adapter);
        ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
        smrRf.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (isEnd) {//has next page
                    MyToast.show("没有更多数据了");
                    loadEnd();
                } else {
                    currentPage++;
                    getData();
                }
            }
        });
        smrRf.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                currentPage = 1;
                isEnd = false;
                getData();
            }
        });
        smrRf.autoRefresh();
    }

    private void getData() {
        Map<String, String> allmap = new HashMap<>();
        allmap.put("reportCode", reportCode);
        allmap.put("pageNum", currentPage + "");
        allmap.put("pageSize", "20");
        MyHttpRequest.getDefault().postRequest(new TypeToken<LzyResponse<List<ReadEntity>>>() {
        }.getType(), null, Urls.getReportPlayList, SignUtils2.makeSignStr(allmap), false, new ObserverCallback<List<ReadEntity>>() {
            @Override
            public void onSuccess(List<ReadEntity> data) {
                if (currentPage == 1) {
                    list.clear();
                    adapter.notifyDataSetChanged();
                }
                if (smrRf == null || llNone == null) {
                    return;
                }
                int oldIndex = list.size();
                if (data != null) {
                    list.addAll(data);
                    if (currentPage == 1) {
                        adapter.notifyDataSetChanged();
                    } else {
                        adapter.notifyItemRangeChanged(oldIndex, data.size());
                    }
                    smrRf.setVisibility(View.VISIBLE);
                    llNone.setVisibility(View.GONE);
                } else {
                    isEnd = true;
                    if (list.size() == 0) {
                        smrRf.setVisibility(View.GONE);
                        llNone.setVisibility(View.VISIBLE);
                    }

                }
                loadEnd();
            }

            @Override
            public void onError(String errorMsg) {
                loadEnd();
                isEnd = true;
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }
    class ReadAdapter extends BaseQuickAdapter<ReadEntity, BaseViewHolder> {
        private Activity context;
        public ReadAdapter(int layoutResId, Activity context, @Nullable List<ReadEntity> data) {
            super(layoutResId, data);
            this.context = context;
        }

        @Override
        protected void convert(BaseViewHolder helper,ReadEntity item) {
            CircleImageView ivHead = helper.getView(R.id.ivHead);
            RelativeLayout rlBg = helper.getView(R.id.rlBg);
            TextView tvTitle = helper.getView(R.id.tvTitle);
            TextView tvTime = helper.getView(R.id.tvTime);
            GlideUtils.loadAvatar(context,item.getHeadPhotoUrl(),ivHead);
            tvTitle.setText(item.getPlayEmployeeName());
            tvTime.setText(Utils.getAccurateDate(item.getCreateTime()));
            rlBg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Urls.antiShake.check()) {
                        return;
                    }
                    Intent intent = new Intent(context, PersonalHomeActivity.class);
                    intent.putExtra("employeeCode", item.getPlayEmployeeCode());
                    context.startActivity(intent);
                }
            });
        }
    }

    @OnClick(R.id.common_title_back)
    public void onClick() {
        finish();
    }

    private void loadEnd() {
        if (null != smrRf && smrRf.getState() == RefreshState.Refreshing)
            smrRf.finishRefresh(true);
        if (null != smrRf && smrRf.getState() == RefreshState.Loading)
            smrRf.finishLoadMore(true);
    }

}
